﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_MAS_BankAccountTypes : IDAL_FIM_MAS_BankAccountTypes
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        //

        public IList<M_FIM_MAS_BankAccountTypes> GetFull(Nullable<int> bank_acc_type_grp_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_BankAccountTypes> infoList = new List<M_FIM_MAS_BankAccountTypes>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bank_acc_type_grp_seq", bank_acc_type_grp_seq);

                strSQL = "SELECT at.bank_acc_type_seq, at.bank_acc_type_name"
                    + ", at.bank_acc_type_grp_seq, atg.bank_acc_type_grp_name"
                    + ", at.record_status"
                    + ", at.CREATED_BY, at.CREATED_WHEN"
                    + ", at.UPDATED_BY, at.UPDATED_WHEN"
                    + " FROM fim_mas_bank_account_types at"
                    + " LEFT JOIN fim_mas_bank_account_type_groups atg ON at.bank_acc_type_grp_seq = atg.bank_acc_type_grp_seq"
                    + " WHERE at.record_status not like @delete_status"
                    + (bank_acc_type_grp_seq != null ? " and at.bank_acc_type_grp_seq = @bank_acc_type_grp_seq" : "")
                    + " ORDER BY at.bank_acc_type_grp_seq, at.bank_acc_type_name";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();
                        info.bank_acc_type_seq = Convert.ToInt32(drc[i]["bank_acc_type_seq"]);
                        info.bank_acc_type_name = drc[i]["bank_acc_type_name"].ToString();
                        info.bank_acc_type_grp_seq = cn.ConvertNullInt(drc[i]["bank_acc_type_grp_seq"]);
                        info.bank_acc_type_grp_name = drc[i]["bank_acc_type_grp_name"].ToString();

                        info.record_status = drc[i]["RECORD_STATUS"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["CREATED_BY"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["CREATED_WHEN"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["UPDATED_BY"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["UPDATED_WHEN"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_BankAccountTypes> GetList(Nullable<int> bank_acc_type_grp_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_BankAccountTypes> infoList = new List<M_FIM_MAS_BankAccountTypes>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bank_acc_type_grp_seq", bank_acc_type_grp_seq);

                strSQL = "SELECT bank_acc_type_seq, bank_acc_type_name"
                    + " FROM fim_mas_bank_account_types"
                    + " WHERE record_status not like @delete_status"
                    + (bank_acc_type_grp_seq != null ? " and bank_acc_type_grp_seq = @bank_acc_type_grp_seq" : "")
                    + " ORDER BY bank_acc_type_name";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();
                        info.bank_acc_type_seq = Convert.ToInt32(drc[i]["bank_acc_type_seq"]);
                        info.bank_acc_type_name = drc[i]["bank_acc_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_MAS_BankAccountTypes GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_BankAccountTypes info = new M_FIM_MAS_BankAccountTypes();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT at.bank_acc_type_seq, at.bank_acc_type_name"
                    + ", at.bank_acc_type_grp_seq, atg.bank_acc_type_grp_name"
                    + ", at.record_status"
                    + ", at.CREATED_BY, at.CREATED_WHEN"
                    + ", at.UPDATED_BY, at.UPDATED_WHEN"
                    + " FROM fim_mas_bank_account_types at"
                    + " LEFT JOIN fim_mas_bank_account_type_groups atg ON at.bank_acc_type_grp_seq = atg.bank_acc_type_grp_seq"
                    + " WHERE at.bank_acc_type_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bank_acc_type_seq = Convert.ToInt32(drc[0]["bank_acc_type_seq"]);
                    info.bank_acc_type_name = drc[0]["bank_acc_type_name"].ToString();
                    info.bank_acc_type_grp_seq = cn.ConvertNullInt(drc[0]["bank_acc_type_grp_seq"]);
                    info.bank_acc_type_grp_name = drc[0]["bank_acc_type_grp_name"].ToString();

                    info.record_status = drc[0]["RECORD_STATUS"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["CREATED_BY"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["CREATED_WHEN"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["UPDATED_BY"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["UPDATED_WHEN"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_FIM_MAS_BankAccountTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                //dbManager.AddParameters(0, "@bank_acc_type_seq", info.bank_acc_type_seq);
                dbManager.AddParameters(0, "@bank_acc_type_name", info.bank_acc_type_name);
                dbManager.AddParameters(1, "@bank_acc_type_grp_seq", info.bank_acc_type_grp_seq);

                dbManager.AddParameters(2, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@created_by", info.created_by);
                dbManager.AddParameters(4, "@created_when", info.created_when);

                strSQL = "INSERT INTO fim_mas_bank_account_types"
                    + " (bank_acc_type_name, bank_acc_type_grp_seq"
                    + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@bank_acc_type_name"
                    + ", @bank_acc_type_grp_seq"

                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when"
                    + " )";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_MAS_BankAccountTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@bank_acc_type_seq", info.bank_acc_type_seq);
                dbManager.AddParameters(1, "@bank_acc_type_name", info.bank_acc_type_name);
                dbManager.AddParameters(2, "@bank_acc_type_grp_seq", info.bank_acc_type_grp_seq);

                dbManager.AddParameters(3, "@updated_by", info.updated_by);
                dbManager.AddParameters(4, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_bank_account_types"
                    + " SET bank_acc_type_name = @bank_acc_type_name"
                    + ", bank_acc_type_grp_seq = @bank_acc_type_grp_seq"
                    + ", updated_by = @Updated_by"
                    + ", updated_when = @Updated_when"
                    + " WHERE bank_acc_type_seq = @bank_acc_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_FIM_MAS_BankAccountTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@bank_acc_type_seq", info.bank_acc_type_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_bank_account_types"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE bank_acc_type_seq = @bank_acc_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
